//
//  ZYWindSpeedSelectView.m
//  YiLin
//
//  Created by Zyark on 2018/6/7.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYWindSpeedSelectView.h"
#import "ZYViewUtils.h"
#import "ZYImageUtils.h"

@interface ZYWindSpeedSelectView()
@property(nonatomic,strong) UIView *bigBgView;
@property(nonatomic,strong) UIView *bgView;
/**
 风速
 */
@property(nonatomic,strong) UISegmentedControl *control;
@property(nonatomic,strong) UIButton *confirmButton;
@property(nonatomic,assign) BOOL isAlterSelectView;
@end

@implementation ZYWindSpeedSelectView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    _bigBgView = [[UIView alloc] initWithFrame:self.bounds];
    _bigBgView.backgroundColor = RGBA(0, 0, 0, 0.3);
    [self addSubview:_bigBgView];
    
    CGFloat width = self.width * 0.92;
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, width * 0.53)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    _bgView.layer.cornerRadius = 5;
    _bgView.layer.masksToBounds = YES;
    _bgView.centerX = self.centerX;
    _bgView.centerY = self.centerY - _bgView.height / 4;
    
    NSArray *titleArray = @[[language getStringByKey:LC_HOME_WIND_SPEED_TYPE_LOW],
                            [language getStringByKey:LC_HOME_WIND_SPEED_TYPE_MIDDLE],
                            [language getStringByKey:LC_HOME_WIND_SPEED_TYPE_HEIGHT],
                            ];
    _control = [[UISegmentedControl alloc] initWithItems:titleArray];
    _control.frame = CGRectMake(0, 0, width * 0.65, width * 0.13 + 1);
    [_bgView addSubview:_control];
    _control.centerY = _bgView.height / 3;
    _control.centerX = _bgView.width / 2;
    [_control setEnabled:YES];
    _control.selectedSegmentIndex = 0;
    UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
    [_control setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:font} forState:UIControlStateSelected];
    [_control setTitleTextAttributes:@{NSForegroundColorAttributeName:RGBA(153, 153, 153, 1),NSFontAttributeName:font} forState:UIControlStateNormal];
//    _control.layer.cornerRadius = 5;
//    _control.layer.masksToBounds = YES;
    _control.tintColor = kMainColor;
    [_control addTarget:self action:@selector(changeWindSpeedType:) forControlEvents:UIControlEventValueChanged];
    
    //确认
    _confirmButton = [ZYViewUtils setupConfirmBtn];
    _confirmButton.size = CGSizeMake(width / 3, width * 0.12);
    [_bgView addSubview:_confirmButton];
    _confirmButton.centerX = _bgView.width / 2;
    _confirmButton.top = _control.bottom + _control.height * 0.6;
    [_confirmButton addTarget:self action:@selector(doConfirm:) forControlEvents:UIControlEventTouchUpInside];
    [_confirmButton setTitle:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_CONFIRM_BUTTON_TITLE] forState:UIControlStateNormal];
    
    self.alpha = 0;
    self.bigBgView.hidden = YES;
    self.top = -self.height;
    self.backgroundColor = [UIColor clearColor];

}

#pragma mark - Event
-(void)changeWindSpeedType:(UISegmentedControl *)control{
    NSInteger index =  control.selectedSegmentIndex;
    if(index == 0){
        _speedType = ZYWindSpeedTypeLow;
    }else if(index == 1){
        _speedType = ZYWindSpeedTypeMiddle;
    }else{
        _speedType = ZYWindSpeedTypeHeight;
    }
}

-(void)doConfirm:(UIButton *)button{
    if([_delegate respondsToSelector:@selector(windSpeedSelectView:didSelectWithWindSpeedType:)]){
        [_delegate windSpeedSelectView:self didSelectWithWindSpeedType:_speedType];
    }
    [self close];
}

#pragma mark public
-(void)showOrHidden{
    _isAlterSelectView = !_isAlterSelectView;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.bigBgView.hidden = YES;
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        self.bigBgView.hidden = !self.isAlterSelectView;
    }];
}

-(void)close{
    _isAlterSelectView = NO;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.bigBgView.hidden = YES;
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        self.bigBgView.hidden = !self.isAlterSelectView;
    }];
}

#pragma mark - Setter
-(void)setSpeedType:(ZYWindSpeedType)speedType{
    _speedType = speedType;
    switch (speedType) {
        case ZYWindSpeedTypeLow:
            [_control setSelectedSegmentIndex:0];
            break;
        case ZYWindSpeedTypeMiddle:
            [_control setSelectedSegmentIndex:1];
            break;
        case ZYWindSpeedTypeHeight:
            [_control setSelectedSegmentIndex:2];
            break;
        default:
            break;
    }
}

@end
